package com.compoundsystem.compound.controller;
import com.compoundsystem.admin.result.Result;
import com.compoundsystem.compound.pojo.Compound;
import com.compoundsystem.compound.service.CompoundService;
import com.compoundsystem.dto.CompoundQueryDto;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.UUID;


@RestController
public class CompoundController {
     @Autowired
     CompoundService compoundService;

    @PostMapping("/api/compound/list")
    public Result compoundList(@RequestBody CompoundQueryDto compoundQueryDto)
    {
        return new Result(200,"",compoundService.selectCompoundPage(compoundQueryDto));
    }

    @PostMapping("/api/compound/add")
    public Result addCompound(@RequestBody Compound compound){
        return new Result(200,"",compoundService.addCompound(compound));
    }

    @PostMapping("/api/compound/update")
    public Result updateCompound(@RequestBody Compound compound){
        return new Result(200,"",compoundService.updateCompound(compound));
    }


    @PostMapping("/api/compound/delete")
    public Result deleteCompound(Integer id){
        return new Result(200,"",compoundService.deleteCompound(id));
    }


    @PostMapping("/api/compound/delete/batch")
    public Result batchDeleteCompound(@RequestBody List<Integer> ids){
        compoundService.batchDelete(ids);
        return new Result(200,"","");
    }

    @PostMapping("/api/compound/findCompound")
    public Result  findSelectCompound(Integer id){
        return new Result(200,"",compoundService.findSelect(id));
    }


    SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd/");

    @PostMapping("/api/compound/upload")
    public Result uploadImage(@RequestParam("uploadFile") MultipartFile uploadFile, HttpServletRequest req){
        String realPath = req.getSession().getServletContext().getRealPath("/uploadFile/");
        String format = sdf.format(new Date());
        File folder = new File(realPath + format);
        if(!folder.isDirectory()){
            folder.mkdirs();
        }
        String oldName = uploadFile.getOriginalFilename();
        String newName = UUID.randomUUID().toString()+oldName.substring(oldName.lastIndexOf("."));
        try{
            uploadFile.transferTo(new File(folder,newName));
            String filePath = req.getScheme() + "://" + req.getServerName() + ":" + req.getServerPort() + "/uploadFile/"
            + format + newName;
            return new Result(200,"上传成功",filePath);
        } catch (IOException e) {
            e.printStackTrace();
        }
       return  new Result(400,"上传失败","");
    }

}
